import { reqCartList,reqDeleteCartById,reqUpdeteCheckedByid } from "@/api"
const state = {
  cartList:[]
}
const mutations = {
  GETCARTLIST(state,cartList){
    state.cartList = cartList
  }
}
const actions = {
  //获取购物车列表数据
  async getCartList({commit}){//commit占位,因为这个不用带参
    let result = await reqCartList()
    //测试是否能获取个人购物车数据
    // console.log(result)
    if(result.code==200){
      commit("GETCARTLIST",result.data)
    }
  },
  //删除购物车某一个产品
  async deleteCartListBySkuId({commit},skuId){
    let result = await reqDeleteCartById(skuId)
    if(result.code==200){
      return 'ok'  //因为接口没有返回的数据,我们这里只需要知道成功还是失败的结果就行
    }else{
      return Promise.reject(new Error('faile'))
    }
  },
  //修改购物车某一个产品的选中状态
  async updateCheckedById({commit},{skuId,isChecked}){
    let result = await reqUpdeteCheckedByid(skuId,isChecked)
    if(result.code==200){
      return 'ok'
    }else{
      return Promise.reject(new Error('faile'))
    }
  },
  //删除全部勾选的产品
  deleteAllCheckedCart({dispatch,getters}){
    //context:小仓库,commit[提交mutations修改state] getters[计算属性] dispatch[派发action] state[当前仓库数据]
    //获取购物车当中全部的产品(是一个数组)
    // console.log(getters)
    let PromiseAll = []
    getters.cartList.cartInfoList.forEach(item=>{
      let promise = item.isChecked==1?dispatch('deleteCartListBySkuId',item.skuId):''
        //将每一次返回的Promise添加到数组当中 利用Promise.all([p1,p2,p3])
        PromiseAll.push(promise)
    })
    //只要全部的p1|p...都成功,返回结果即为成功
    //如果有一个失败,返回即为失败结果
    return Promise.all(PromiseAll)
  },
  //修改全部产品的状态
  updateAllCartIsChecked({dispatch,state},isChecked){
    // console.log(state,isChecked)
    //数组
    let promiseAll = []
    state.cartList[0].cartInfoList.forEach((item)=>{
      let promise = dispatch('updateCheckedById',{skuId:item.skuId,isChecked})
      promiseAll.push(promise)
    })
    //最终返回的结果
    return Promise.all(promiseAll)
  }
}

const getters = {
  cartList(state){
    return state.cartList[0]||{}
  }
}
export default{
  state,
  mutations,
  actions,
  getters
}